<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/26
 * Time: 17:48
 */

namespace app\zucai\home;

use app\index\controller\Home;
use think\Db;

class Pay extends Home
{
    protected $appid = '3076213256';
    protected $appkey = '9e8505c63732dc41fe4791ba93247f4a';

    public function index()
    {
        $ispost = $this->request->isPost();
        if (!$ispost) return returnError('请使用POST方式请求');

        $post = $this->request->post();
        if (!$post) return returnError('未获取到POST数据');

        if (!(isset($post['user_id']) && $post['user_id'])) return returnError('未获取到user_id');
        if (!(isset($post['money']) && $post['money'])) return returnError('未获取到money');
        if (!(isset($post['type']) && $post['type'])) return returnError('未获取到type');

        $user_id = trimall($post['user_id']);//用户ID
        $money = trimall($post['money']);//充值金额
        $type = trimall($post['type']);//支付方式 1：支付宝， 3：微信支付

        $data_post['uid'] = $user_id;
        $data_post['money'] = $money;
        switch ($type) {
            case 1:
                $data_post['type'] = 'alipay';
                break;
            case 3:
                $data_post['type'] = 'weixin';
                break;
            default:
                return returnError('未开通此支付通道');
        }
        $curl = curl_post_https($_SERVER['SERVER_NAME'] . ':' . $_SERVER["SERVER_PORT"] . url('put/index'), $data_post);
         $curl=trim($curl, "\xEF\xBB\xBF");
		$curl = json_decode($curl);
        if ($curl->code != 200) return returnError($curl->message);

        $pay = $this->pay($curl->data, $money, $type);

        return $pay;
    }

    protected function pay($order, $money, $type)
    {
        $data['appid'] = $this->appid;
        $data['data'] = $order;
        $data['money'] = $money;
        $data['type'] = $type;
        $data['uip'] = $_SERVER["REMOTE_ADDR"];
        $data['token'] = md5("appid=" . $data['appid'] . "&data=" . $data['data'] . "&money=" . $data['money'] . "&type=" . $data['type'] . "&uip=" . $data['uip'] . "&appkey=" . $this->appkey);

        $curl = curl_post_https('http://yunpay.waa.cn/', $data);
        $curl=trim($curl, "\xEF\xBB\xBF");
		$curl = json_decode($curl);

        if ($curl->text == 10088) {
			
            switch ($type) {
                case 3:
                    $qrcode = $this->qrcode($curl->qrcode, $data['data']);
                    $call['qrcode'] = $qrcode;//二维码图片地址
                    break;
                case 1:
                    $call['qrcode'] = $curl->qrcode;
                    break;
                default:
                    return returnError('未开通此支付通道');
            }
			
            $call['order'] = $curl->data;//订单号
            $call['money'] = $curl->money;//金额
            $call['time'] = $curl->times - time();//订单时间
            $call['status'] = $curl->orderstatus;//订单状态
            return returnOk($call);
        } else {
            return returnError('发起支付失败');
        }
    }


    protected function qrcode($url, $name)
    {
        $href = 'qrcode/' . $name . '.png';
        vendor('phpqrcode/phpqrcode');
        \QRcode::png($url, $href);
        return $href;
    }


    /**
     * @return \think\response\Json
     * 支付回调
     * gxq <know1111@vip.qq.com>
     */
    public function payCallback()
    {
        $ddh = $_POST['ddh']; //支付宝,微信，QQ钱包 订单号

        $key = $_POST['key']; //APPKEY验证，也可以使用签名在软件中开启

        $name = $_POST['name']; //备注信息  接收网关data 参数  支付订单号

        $lb = $_POST['lb']; //分类 =1 支付宝 =2财付通 =3 微信

        $money = $_POST['money'];//金额

        $paytime = $_POST['paytime'];//充值时间

        $key2 = $this->appkey;//APPKEY 和云端和软件上面保持一致

        //使用签名，如果使用签名用 $sing对比  需要在软件配置中  使用签名 钩上
        $sing =md5('ddh='.$ddh.'&name='.$name.'&money='.$money.'&key='.$key2.'');

        if($key==$key2){//直接对比appkey是否正确
            //if($key==$sing){//使用签名对比key是否正确
            //判断支付来源
            if($lb==1) $leibie='支付宝';//可根据网站自定义数据
            if($lb==2) $leibie='财付通QQ钱包';//可根据网站自定义数据
            if($lb==3) $leibie='微信支付';//可根据网站自定义数据
            /*
           此处执行你的程序逻辑 回执成功后
           1、可以做成 判断支付宝订单号是否存在来完成充值
           2、还可以做成 判断网站订单号(name)来完成充值
           3、请做好订单号充值判断
           */
            $data_post['order_id']=$name;
            $data_post['money']=$money;
            $curl = curl_post_https($_SERVER['SERVER_NAME'] . ':' . $_SERVER["SERVER_PORT"] . url('put/put'), $data_post);
           $curl=trim($curl, "\xEF\xBB\xBF");
		  $curl = json_decode($curl);

            if ($curl->code==200){
				if(file_exists("qrcode/".$name.".png"))unlink("qrcode/".$name.".png");
            //执行完毕回执输出ok 字符
            echo "ok";
            exit;
            }else{
                echo "error";
                exit;
            }
        }else{
            //密匙错误
            echo 'appkey error';
            exit;
        }
        //上传到网站后 请登陆云端 将异步通知地址改写正确 例如 ：http://www.abc.com/callback.php 然后重启软件
    }
}